package java实验六;

import java.util.Scanner;

public class 程序四 {

    public static void main(String[] args) {
        // Read a Stock solution from the console
        int[][] grid = readASolution();

        // Check whether the solution is valid
        System.out.println(isValid(grid) ? "Valid solution" : "Invalid solution");
    }

    // Read a Stock puzzle solution from the console
    public static int[][] readASolution() {
        // Create a Scanner
        Scanner input = new Scanner(System.in);

        // Prompt the user to enter a Stock puzzle solution:
        System.out.println("Enter a Sudoku puzzle solution:");
        int[][] grid = new int[9][9];

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                grid[i][j] = input.nextInt();
            }
        }
        input.close();
        return grid;
    }

    // Check whether a solution is valid
    public static boolean isValid(int[][] grid) {
        for (int i = 0; i < 9; i++) {
            // Check whether each row has unique values
            for (int column = 0; column < 9; column++) {
                // Check whether grid[i][column] is unique in i's row
                for (int j = 0; j < 9; j++) {
                    if (i != j && grid[i][column] == grid[j][column]) {
                        return false;
                    }
                }
            }

            // Check whether each column has unique values
            for (int row = 0; row < 9; row++) {
                // Check whether grid[row][i] is unique in j's column
                for (int j = 0; j < 9; j++) {
                    if (row != i && grid[row][j] == grid[i][j]) {
                        return false;
                    }
                }
            }
        }

        // Check whether each 3-by-3 box has unique values
        for (int row = 0; row < 9; row += 3) {
            for (int col = 0; col < 9; col += 3) {
                // Check whether grid[i][j] is unique in the 3-by-3 box
                for (int r = row; r < row + 3; r++) {
                    for (int c = col; c < col + 3; c++) {
                        int i = 0;
						int j = 0;
						if ((r != i || c != j) && grid[r][c] == grid[i][j]) {
                            return false;
                        }
                    }
                }
            }
        }

        return true; // The current value at grid[i][j] is valid
    }
}
